package edu.umd.fosite.demos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;

/**
 * Entry point for running the various demos.
 *
 */
public class DemoRunner {

	private static Vector<Class<? extends Demo>> demos = new Vector<>();

	static {
		demos.add(LightningDemo.class);
		demos.add(TreeDemo.class);
		demos.add(TerrainDemo.class);
		demos.add(RaptorDemo.class);
		demos.add(AudioDemo.class);
	}

	public static void main(String[] args) {
		final JFrame frame = new JFrame("Fosite Demos");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);

		final JComboBox<Class<? extends Demo>> comboBox = new JComboBox<>(demos);

		JPanel outer = new JPanel();
		BoxLayout outerLayout = new BoxLayout(outer, BoxLayout.Y_AXIS);
		outer.setLayout(outerLayout);
		outer.add(comboBox);

		JPanel buttons = new JPanel();
		BoxLayout buttonsLayout = new BoxLayout(buttons, BoxLayout.X_AXIS);
		buttons.setLayout(buttonsLayout);
		JButton run = new JButton("Run Demo");
		buttons.add(run);
		run.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int selected = comboBox.getSelectedIndex();
				Class<? extends Demo> demoClass = comboBox.getItemAt(selected);
				Demo demo;
				try {
					demo = demoClass.newInstance();
					demo.run();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		JButton cancel = new JButton("Cancel");
		buttons.add(cancel);
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				System.exit(0);
			}
		});
		outer.add(buttons);

		frame.setContentPane(outer);

		frame.pack();
		frame.setVisible(true);
	}
}
